package org.example.everyday;

import java.util.Arrays;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * Copyright: 天津联想协同科技有限公司
 * <p>
 *
 * @author anlu
 * @date 2022/4/19 11:02
 */
public class Lc821 {
    public static void main(String[] args) {
        Lc821 lc821 = new Lc821();

        String s = "loveleetcode";
        char c = 'e';
        int[] res = lc821.shortestToChar(s,c);
        for (int i = 0; i < res.length; i++) {
            System.out.println(res[i]);
        }

    }
    public int[] shortestToChar(String s, char c) {
        int n = s.length();
        int[] ans = new int[n];
        Arrays.fill(ans,n + 1);
        for (int i = 0,j = -1; i < n; i++) {
            if(s.charAt(i) == c){
                j  = i;
            }
            if(j != -1){
                ans[i] = i - j;
            }
        }
        for(int i = n - 1, j = -1; i >= 0; i--){
            if(s.charAt(i) == c){
                j = i;
            }
            if(j != -1){
                ans[i] = Math.min(ans[i], j - i);
            }
        }
        return ans;
    }
}
